package com.cjl.web.backend;

import com.cjl.entity.EbComment;
import com.cjl.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping( "/comment" )
public class CommentController {
    @Autowired
    private CommentService commentService;
    @RequestMapping("/list" )
    public String list(Model model,@RequestParam(value="page",defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize",defaultValue ="3") int pageSize) {//alt+enter
        System.out.println("start service.....;psize==>"+psize);
        List<EbComment> commentList = commentService.listByPage(psize,3);
        final Integer totalPage = commentService.findTotalPage(pageSize);
        model.addAttribute(  "commentList" , commentList);
        //3.2 mode l中绑定总页数
        model.addAttribute( "totalPage" , totalPage);
        //3.3 model中绑定当前页
        model.addAttribute (  "currPage" ,psize);
        //3.4 jsp
        return "manage/comment";//manage/comment.jsp
    }
    @RequestMapping("/to_edit")
    public String toEdit(Model model,
                         @RequestParam("ecId") int ecId) {
        System.out.println("点击修改跳过来了,ecId=" + ecId);
        //1.调用service层的方法查询点击的新闻的所有信息
        EbComment ebComment = commentService.selectOne(ecId);
        //2.指定model和jsp
        model.addAttribute("ebComment", ebComment);
        return "manage/guestbook-modify";//manage/comment-modify.jsp
    }
    @RequestMapping("/edit")
    public String edit(EbComment ebComment) {
        System.out.println("ebComment==>" + ebComment);
        //1.调用service层的更新新闻的方法
        Integer result = commentService.updateComment(ebComment);
        //2.根据更新返回过来的结果决定跳转页面
        if (result < 1) {
            //更新失败的情况
            return "manage/manage-error";
        }
        //更新成功的情况
        return "manage/manage-result";
    }
    @RequestMapping("/delete")
    public String delete(Integer ecId){
        //1.参数校验

        //2.执行删除（真实项目中 delete from 表名 where id=?）
        int result =commentService.deleteByPk(ecId);
        if(result==0){
            //TODO 要么return错误页面 要么跑出异常
        }
        return "manage/manage-result";
    }
}